package io.demo.message.domain.proto;

import io.demo.message.domain.proto.Customer.GenderType;
import java.util.ArrayList;
import java.util.List;

/**
 * @author imlijinting
 */
public class CustomerMessageConverter implements
    MessageConverter<io.demo.message.domain.Customer, Customer> {

  @Override
  public Customer toMessage(io.demo.message.domain.Customer pojo) {

    List<Address> list = new ArrayList<>();

    pojo.getAddresses().forEach(address ->
        list.add(MessageConverters.toMessage(address, io.demo.message.domain.Address.class)));

    return Customer.newBuilder()
        .setId(pojo.getId())
        .setGenderType(GenderType.valueOf(pojo.getGenderType().name()))
        .setFirstName(pojo.getFirstName())
        .setLastName(pojo.getLastName())
        .addAllAddresses(list)
        .build();
  }

  @Override
  public io.demo.message.domain.Customer fromMessage(Customer message) {
    List<io.demo.message.domain.Address> addresses = new ArrayList<>();

    message.getAddressesList().forEach(address ->
        addresses.add(
            MessageConverters.fromMessage(address, io.demo.message.domain.Address.class)));

    return io.demo.message.domain.Customer.builder()
        .id(message.getId())
        .genderType(io.demo.message.domain.GenderType.valueOf(message.getGenderType().name()))
        .addresses(addresses)
        .lastName(message.getLastName())
        .firstName(message.getFirstName())
        .build();
  }
}
